<?php

namespace Project\Admin\Models;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class AdminUser extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $appends = ['roles', 'roles_name'];
    
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getRolesAttribute()
    {
        $roles = \Project\Admin\Models\AdminUserRole::where('uid', $this->attributes['id'])->pluck('role_id');
        return $roles->toArray();
    }

    public function getRolesNameAttribute()
    {
        $roles = \Project\Admin\Models\AdminUserRole::where('uid', $this->attributes['id'])->pluck('role_id');
        return \Project\Admin\Models\AdminRole::whereIn('id', $roles->toArray())->pluck('role_name', 'id')->toArray();
    }
}
